package com.sxg.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxg.convert.SxgGoodsInfoConvert;
import com.sxg.dao.SxgGoodsInfoDao;
import com.sxg.entity.SxgGoodsDetailEntity;
import com.sxg.entity.SxgGoodsInfoEntity;
import com.sxg.entity.SxgImagesEntity;
import com.sxg.query.SxgGoodsInfoQuery;
import com.sxg.service.SxgGoodsDetailService;
import com.sxg.service.SxgGoodsInfoService;
import com.sxg.service.SxgImagesService;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgGoodsInfoVO;
import com.sxg.vo.resp.GoodsDetailsRespVo;
import com.sxg.vo.resp.GoodsInfoRespVo;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品信息
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@Service
@AllArgsConstructor
public class SxgGoodsInfoServiceImpl extends ServiceImpl<SxgGoodsInfoDao, SxgGoodsInfoEntity> implements SxgGoodsInfoService {

    @Autowired
    private SxgGoodsDetailService sxgGoodsDetailService;

    @Resource
    private SxgImagesService sxgImagesService;

    @Override
    public AjaxResult page(SxgGoodsInfoQuery query) {
        List<GoodsInfoRespVo> respVos = getBaseMapper().page(query.getCategoryId(), (query.getPage() - 1) * query.getLimit(), query.getLimit());
        Integer count = getBaseMapper().count();
        return AjaxResult.success(respVos).put("total", count);
    }

    private LambdaQueryWrapper<SxgGoodsInfoEntity> getWrapper(SxgGoodsInfoQuery query) {
        LambdaQueryWrapper<SxgGoodsInfoEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(SxgGoodsInfoVO vo) {
        SxgGoodsInfoEntity entity = SxgGoodsInfoConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SxgGoodsInfoVO vo) {
        SxgGoodsInfoEntity entity = SxgGoodsInfoConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public GoodsDetailsRespVo selectDetailById(Long id) {

        GoodsDetailsRespVo goodsDetailsRespVo = new GoodsDetailsRespVo();

        SxgGoodsInfoEntity sxgGoodsInfoEntity = baseMapper.selectOne(Wrappers.<SxgGoodsInfoEntity>lambdaQuery().eq(SxgGoodsInfoEntity::getId, id));
        BeanUtils.copyProperties(sxgGoodsInfoEntity, goodsDetailsRespVo);

        SxgGoodsDetailEntity sxgGoodsDetailEntity = sxgGoodsDetailService.getByGoodsId(id);
        BeanUtils.copyProperties(sxgGoodsDetailEntity, goodsDetailsRespVo);

        List<SxgImagesEntity> imagesEntityList = sxgImagesService.getByGoodsId(id);
        goodsDetailsRespVo.setImages(imagesEntityList);

        return goodsDetailsRespVo;
    }

}